//
//  SelectFriendCellCell.swift
//  MoyoungDevice
//
//  Created by 易先生 on 2022/9/3.
//

import UIKit
import CRPEarPhone
import EarbudsDatabase

class SelectFriendCell: TableViewCell {

	override func makeUI() {
		super.makeUI()

		contentView.addSubview(headImage)
		contentView.addSubview(titleLabel)
		contentView.addSubview(phoneLabel)
		contentView.addSubview(iconImage)

		headImage.snp.makeConstraints { make in
			make.leading.equalToSuperview().offset(20)
			make.centerY.equalToSuperview()
			make.size.equalTo(CGSize(width: 40, height: 40))
		}

		iconImage.snp.makeConstraints { make in
			make.trailing.equalToSuperview().offset(-20)
			make.centerY.equalToSuperview()
			make.size.equalTo(CGSize(width: 20, height: 20))
		}

		phoneLabel.snp.makeConstraints { make in
            make.trailing.equalTo(iconImage.snp.leading).offset(-10)
			make.centerY.equalToSuperview()
			make.width.equalTo(120)
		}

		titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(headImage.snp.trailing).offset(10)
			make.trailing.equalTo(phoneLabel.snp.leading).offset(-10)
			make.top.equalToSuperview().offset(20)
			make.bottom.equalToSuperview().offset(-20)
		}
	}

	public func configModel(contact: ContactModel?) {
		setContactModel(contact: contact)
		self.iconImage.isHidden = true
	}

	private func setContactModel(contact: ContactModel?) {
        
        if let contact = contact {
            self.titleLabel.text = contact.fullName
            self.phoneLabel.text = contact.selectPhoneNumber
            if let imageData = contact.imageData {
                self.headImage.image = UIImage(data: imageData)
            } else {
                if let avatar = UIImage().createAvatarWith(name: contact.fullName, color: R.color.settings_1_contacts_bg() ?? UIColor.black) {
                    self.headImage.image = avatar
                }
            }
        }
		
		
	}

	public func configSelect(currentSelectContact: [ContactModel], contact: ContactModel) {

		setContactModel(contact: contact)
		if currentSelectContact.first(where: { $0.phoneNumbers == contact.phoneNumbers && $0.fullName == contact.fullName && $0.selectPhoneNumber == contact.selectPhoneNumber }) != nil {
			self.iconImage.isHidden = false
		} else {
			self.iconImage.isHidden = true
		}
	}

	private lazy var headImage: ImageView = {
		let headImage = ImageView()
		headImage.contentMode = .scaleAspectFit
		headImage.layer.cornerRadius = 20
		headImage.layer.masksToBounds = true
		return headImage
	}()

	private lazy var titleLabel: Label = {
		let titleLabel = Label()
        titleLabel.textColor = R.color.common_3_text()
		titleLabel.font = Fonts.systemFont(ofSize: 16)
		titleLabel.numberOfLines = 2
		return titleLabel
	}()

	private lazy var phoneLabel: Label = {
		let phoneLabel = Label()
        phoneLabel.textColor = R.color.common_23_text_second()
        phoneLabel.font = Fonts.systemFont(ofSize: 14)
		return phoneLabel
	}()

	public lazy var iconImage: UIImageView = {
		let iconImage = UIImageView()
		iconImage.image = R.image.ic_common_pick()
		iconImage.isHidden = true
		return iconImage
	}()
}
